import {fileURLToPath, URL} from "node:url";

import {defineConfig, loadEnv} from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";

// https://vitejs.dev/config/
export default defineConfig(({command, mode, ssrBuild}) => {
    const env = loadEnv(mode, "./");
    if (command === "serve") {
        return {
            plugins: [vue(), vueJsx()],
            resolve: {
                alias: {
                    "@": fileURLToPath(new URL("./src", import.meta.url))
                }
            },
            define: {
                "process.env": {
                    baseURL: env.VITE_APP_BASE_API
                }
            },
            server: {
                // host: "127.0.0.1",
                open: true, //启动项目后打开浏览器
                // port: 80, //端口
                proxy: {
                    "/api": {
                        target: env.VITE_REQUEST_BASE_URL, //API服务地址
                        changeOrigin: true,
                        rewrite: path => path.replace(/^\/api/, '/api') // 去掉 /api 前缀
                    }
                }
            }
        };
    } else {
        return {
            plugins: [vue(), vueJsx()],
            base: env.VITE_APP_BASE_API,
            resolve: {
                alias: {
                    "@": fileURLToPath(new URL("./src", import.meta.url))
                }
            }
        };
    }
});
